package com.reptile.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.reptile.dto.NewsDTO;
import com.reptile.enums.YsBaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 爬取编程技术核心类
 *
 * @author songyafei
 */
@Slf4j
@Component
public class TechnologyDataUtil {

    /**
     * CSDN-热门话题
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> CSDNNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".topic_ul").select("li");
        List<NewsDTO> result = Lists.newArrayList();
        for (Element nextChild : elements) {
            //url
            String url = nextChild.select("a[href]").attr("href");
            //img
            String img = nextChild.select("img[src]").attr("src");
            Elements child = nextChild.select(".info");
            NewsDTO newsDTO = NewsDTO.builder().build();
            for (Element childs : child) {
                String contentTitle = childs.select("h2").text();
                String contentValue = childs.select("p").text().split("？")[0];
                newsDTO.setTitle(contentTitle);
                newsDTO.setContent(contentValue);
            }
            newsDTO.setUrl(url);
            newsDTO.setNewsDate(LocalDate.now());
            newsDTO.setCrawlingDate(LocalDate.now());
            newsDTO.setPictureUrl(img);
            newsDTO.setNewType(YsBaseEnum.NEWS_TYPE_IT_CSDN_HOT_HT.getCode());
            result.add(newsDTO);
        }
        log.info("最终对象{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 开源中国-开源问答
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> KYAnswersNewsParseList(Document document) {
        Elements elements = document.select(".article-list").select(".ui");
        List<NewsDTO> result = Lists.newArrayList();
        Elements item = elements.select(".question-item");
        for (Element items : item) {
            //获取url
            String url = items.select(".content").select("a[href]").attr("href");
            //获取标题
            String title = items.select(".content").select("a[href]").text();
            //获取文本
            String content = items.select(".description").select("p").text();
            //img
            String img = items.select("img").attr("src");
            NewsDTO newsDTO = NewsDTO.builder()
                    .title(title).url(url).crawlingDate(LocalDate.now())
                    .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_JS.getCode())
                    .content(content).pictureUrl(img)
                    .build();
            result.add(newsDTO);
        }
        log.info("开源中国技术问答数据{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 开源中国-数据库专区
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> KYDatabaseNewsParseList(Document document) {
        //根据网页标签解析源码
        Elements elements = document.select(".entry-list-box").select(".article-item__body");
        List<NewsDTO> result = Lists.newArrayList();
        //获取新浪新闻要闻标签数据
        for (Element item : elements) {
            //获取url标签
            String url = item.select(".article-item__title").select("a[href]").attr("href");
            //获取标题
            String title = item.select("a[title]").attr("title");
            //获取文本
            String content = item.select("p").text();

            NewsDTO newsDTO = NewsDTO.builder()
                    .title(title).url(url).crawlingDate(LocalDate.now())
                    .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_JS.getCode())
                    .content(content)
                    .build();
            result.add(newsDTO);
        }
        log.info("开源中国数据库数据专区{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 开源中国-软件更新专区
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> KYSoftwareUpdateNewsParseList(Document document) {
        Elements elements = document.select(".ui").select(".news-item");
        List<NewsDTO> result = Lists.newArrayList();
        //获取新浪新闻要闻标签数据
        for (Element item : elements) {
            //获取url标签
            String url = item.select(".news-item-border").attr("data-url");
            //获取标题
            String title = item.select(".title").text();
            //获取文本
            String content = item.select(".description").select("p").text();
            //img
            String img = item.select("a[href]").attr("href");
            if (StringUtils.isNoneBlank(img) && StringUtils.isNoneBlank(content)) {
                NewsDTO newsDTO = NewsDTO.builder()
                        .title(title).url(url).crawlingDate(LocalDate.now())
                        .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_JS.getCode())
                        .content(content).pictureUrl(img)
                        .build();
                result.add(newsDTO);
            }
        }
        return result;
    }

    /**
     * 开源中国-开发技能专区
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> KYDevelopingSkillsNewsParseList(Document document) {
        Elements elements = document.select(".entry-list-box").select(".entry-item__inner");
        List<NewsDTO> result = Lists.newArrayList();
        //获取新浪新闻要闻标签数据
        for (Element item : elements) {
            //获取url标签
            String url = item.select(".article-item__title").select("a[href]").attr("href");
            //获取标题
            String title = item.select(".article-item__title").select("a[href]").attr("title");
            //获取文本
            String content = item.select(".article-item__content").select("p").text();
            if (StringUtils.isNoneBlank(url) && StringUtils.isNoneBlank(content)) {
                NewsDTO newsDTO = NewsDTO.builder()
                        .title(title).url(url).crawlingDate(LocalDate.now())
                        .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_JS.getCode())
                        .build();
                result.add(newsDTO);
            }
        }
        return result;
    }

    /**
     * 开源中国-综合资讯专区
     *
     * @param document
     * @return List<NewsDTO>
     */
    public static List<NewsDTO> KYComprehensiveInfoNewsParseList(Document document) {
        Elements elements = document.select(".news-list-container").select(".news-item");
        List<NewsDTO> result = Lists.newArrayList();
        //获取新浪新闻要闻标签数据
        for (Element item : elements) {
            //获取url标签
            String url = item.attr("data-url");
            //获取标题
            String title = item.select(".title").attr("title");
            //获取文本
            String content = item.select(".description").select("p").text();
            if (StringUtils.isNoneBlank(url) && StringUtils.isNoneBlank(content)) {
                NewsDTO newsDTO = NewsDTO.builder()
                        .title(title).url(url).crawlingDate(LocalDate.now())
                        .newsDate(LocalDate.now()).newType(YsBaseEnum.NEWS_TYPE_JS.getCode())
                        .build();
                result.add(newsDTO);
            }
        }
        log.info("数据{}",JSON.toJSONString(result));
        return result;
    }
}
